package com.mjh.zhujianju.util.party;

import com.google.common.collect.Lists;
import com.mjh.zhujianju.model.party.BuildingExamAnswerEO;
import com.mjh.zhujianju.model.party.BuildingExamTopicEO;
import com.mjh.zhujianju.vo.party.BuildingExamTopicVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuyang
 * @Description: 处理考试相关的工具类
 * @date 2018/8/19 20:59
 */
@Component
public class ExamUtil {

    /**
     * 根据 试题集合、内容结合 组装返回 题目+ 试题 集合
     * @param buildingExamTopicEOList  题目集合
     * @param buildingExamAnswerEOList 试题集合
     * @return 试题+题目集合
     */
    public List<BuildingExamTopicVO> initTopicAnswers(List<BuildingExamTopicEO> buildingExamTopicEOList, List<BuildingExamAnswerEO> buildingExamAnswerEOList){
        List<BuildingExamTopicVO> list = Lists.newArrayList();
        BuildingExamTopicVO vo;
        List<BuildingExamAnswerEO> answerEOList;
        for (BuildingExamTopicEO buildingExamTopic : buildingExamTopicEOList){
            vo = new BuildingExamTopicVO();
            BeanUtils.copyProperties(buildingExamTopic,vo);
            answerEOList = Lists.newArrayList();
            for (BuildingExamAnswerEO buildingExamAnswerEO : buildingExamAnswerEOList){
                if (vo.getId().equals(buildingExamAnswerEO.getTopicId())){
                    answerEOList.add(buildingExamAnswerEO);
                }
            }
            vo.setBuildingExamAnswerEOList(answerEOList);
            list.add(vo);
        }
        return list;
    }


    /**
     *  客户的答题记录与正确答案匹配
     *  if (匹配上 ) 加当前题目分数
     * @param trueList 正确答案
     * @param userAnswers 用户答题记录
     * @return 总分数
     */
    public Integer countPoints(List<BuildingExamTopicVO> trueList, Map<Long,List<Long>> userAnswers){
        Integer total = 0;
        for (Long topicId : userAnswers.keySet()){
            List<Long> answerIds = userAnswers.get(topicId);
            //多选
            if (answerIds.size()>1){
                boolean ischeak = false;
                Integer topicTotal = 0;
                for(Long answerId : answerIds){
                    for (BuildingExamTopicVO buildingExamTopicVO : trueList){
                        //获取当前题目的分数
                        topicTotal = buildingExamTopicVO.getGrade();

                        //题目id == 答案题目id
                        boolean b1 = topicId.equals(buildingExamTopicVO.getId());

                        //用户答案id != 正确答案id
                        boolean b2 = !answerId.equals(buildingExamTopicVO.getAnswerId());
                        if (b1 && b2){
                            ischeak = true;
                            continue;
                        }
                    }
                    if (ischeak){
                        continue;
                    }
                }
                //如果没有触发不等条件，则加分
                if (!ischeak){
                    total = total+topicTotal;
                }
            }else {
                //单选
                for (BuildingExamTopicVO buildingExamTopicVO : trueList){
                    if (topicId.equals(buildingExamTopicVO.getId())){
                        if (answerIds.get(0).equals(buildingExamTopicVO.getAnswerId())){
                            total = total+buildingExamTopicVO.getGrade();
                            continue;
                        }
                    }
                }
            }
        }
        return total;
    }

    public Map<String,Integer> analysisuserResult(List<BuildingExamTopicVO> buildingExamTopicVOList, Map<Long,List<Long>> userAnswers){
        Integer total = 0;
        Integer passed = 0;
        Map<String,Integer> resultMap = new HashMap<String,Integer>();
        for (Long topicId : userAnswers.keySet()){
            List<Long> answerIds = userAnswers.get(topicId);
            //多选
            if (answerIds.size()>1){
                boolean ischeak = false;
                Integer topicTotal = 0;
                for(Long answerId : answerIds){
                    for (BuildingExamTopicVO buildingExamTopicVO : buildingExamTopicVOList){
                        //获取当前题目的分数
                        topicTotal = buildingExamTopicVO.getGrade();

                        //题目id == 答案题目id
                        boolean b1 = topicId.equals(buildingExamTopicVO.getId());

                        //用户答案id != 正确答案id
                        boolean b2 = !answerId.equals(buildingExamTopicVO.getAnswerId());
                        if (b1 && b2){
                            ischeak = true;
                            continue;
                        }
                    }
                    if (ischeak){
                        continue;
                    }
                }
                //如果没有触发不等条件，则加分
                if (!ischeak){
                    total = total+topicTotal;
                    passed++;
                }
            }else {
                //单选
                for (BuildingExamTopicVO buildingExamTopicVO : buildingExamTopicVOList){
                    if (topicId.equals(buildingExamTopicVO.getId())){
                        if (answerIds.get(0).equals(buildingExamTopicVO.getAnswerId())){
                            total = total+buildingExamTopicVO.getGrade();
                            passed++;
                            continue;
                        }
                    }
                }
            }
        }

        resultMap.put("total",total);
        resultMap.put("passed",passed);
        resultMap.put("unpassed",buildingExamTopicVOList.size()-passed);
        return resultMap;
    }

    /*
     *计算time2减去time1的差值 差值只设置 几天 几个小时 或 几分钟
     * 根据差值返回多长之间前或多长时间后
     * */
    public  String getDistanceTime(long time1, long time2) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long diff;

        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        day = diff / (24 * 60 * 60 * 1000);
        hour = (diff / (60 * 60 * 1000) - day * 24);
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        if (day != 0){
            return day + "天"+hour + "小时"+min + "分钟" + sec + "秒";
        }
        if (hour != 0){
            return hour + "小时"+ min + "分钟" + sec + "秒";
        }
        if (min != 0) {
            return min + "分钟" + sec + "秒";
        }
        if (sec != 0){
            return sec + "秒" ;
        }
        return "0秒";
    }

}
